<?php
class Dept extends Controller {
	function Dept() {
		parent::Controller();
		$this->load->helper("url");
		$this->data["base"] = $this->config->item('base_url');
		$this->load->model('Deptmodel','',TRUE);
		$this->load->model('TransLog');
		$this->load->database();	
                session_start();

	}
	function index() {
		if(((isset($_SESSION['sys_admin'])) and isset($_SESSION['name']))||((isset($_SESSION['admin']))and isset($_SESSION['name']))||((isset($_SESSION['intern_audit']))and isset($_SESSION['name']))||((isset($_SESSION['second_consult']))and isset($_SESSION['name']))||((isset($_SESSION['dept_pic']))and isset($_SESSION['name']))) {
			$this->data['title'] = 'Manage Departemen';
			$this->data['h2_title'] = 'MANAGE DEPARTEMEN';
			$this->data['dept'] = $this->Deptmodel->show_all_dept();
			$this->data['prod'] = $this->Deptmodel->sProd();
			$this->data['link'] = array('link_add'=>anchor('master/dept/create_dept','Create Departemen',array('class' =>'add')));
			
			$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('dept/dept_update');
			$this->load->view('layout/slice_bawah');		
		} else {
			redirect('main');
		}
		
	}
	function deptr() {
		$this->data['title'] = 'Manage Departemen';
		$this->data['h2_title'] = 'MANAGE DEPARTEMEN';
		$this->data['dept'] = $this->Deptmodel->show_all_dept();
		/*$dep = $this->Deptmodel->show_all_dept()->result();
		
		$tmpl = array('table_open' =>'<table border="0" cellpadding="0" cellspacing="0" width="560">','row_alt_start' =>'<tr class="tb-jd">','row_alt_end' =>'</tr>');
			$this->table->set_template($tmpl);
			
			//set heading untul tabel
			$this->table->set_empty("&nbsp;");
			$this->table->set_heading('No','Dept ID','Dept Name','Descr ','Actions');
			$i=0;
			foreach($dep as $dept) {
				$adesc = substr($dept->descr,0,200);
				$this->table->add_row(++$i,$dept->dept_id,$dept->dept_name,$adesc ,
				anchor('master/dept/update_dept/'.$dept->dept_id,'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',array('class'=>'update')).'&nbsp;'.
				anchor('master/dept/delete/'.$dept->dept_id,'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',array('class'=>'delete','onclick'=>"return confirm('Anda yakin akan menghapus data ini?')")));
			}
			
			$this->data['table'] = $this->table->generate();*/
			
			$this->data['prod'] = $this->Deptmodel->sProd();
			$this->data['link'] = array('link_add'=>anchor('master/dept/create_dept','Create Departemen',array('class' =>'add')));
			
			$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('dept/dept_update');
			$this->load->view('layout/slice_bawah');				
			
	}
	
	function update_dept($id) {
		$this->data['title'] ='Manage Dept';
		$this->data['show'] = $this->Deptmodel->show_all_dept()->result();
		$x = $this->Deptmodel->update_prod($id)->row();
		$this->data['h2_title'] = '';
		$this->data['form_action'] = site_url('master/dept/update_process');
		
		$this->data['default']['dept_id'] = $x->dept_id;
		$this->data['default']['dept_name'] = $x->dept_name;
		
		$this->data['default']['descrx'] = $x->descr;
		$this->data['descrx'] = $x->descr;
		
		$this->data['link'] = array('link_back' =>anchor('master/dept','kembali',array('class' => 'back')));
		$this->data['prod'] = $this->Deptmodel->sProd();
		$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('dept/dept');
			$this->load->view('layout/slice_bawah');
		
		
	}
	
	function update_process() {
		$id = $this->input->post('dept_id');
		if(isset($_POST['submit'])) {	
			$depts = array('dept_name'=>$this->input->post('dept_name'),
							
							'descr'=>$this->input->post('descr'));
							
			//update this db
			
			$this->Deptmodel->deptname($id,$depts);
			
			$this->session->set_flashdata('message', 'Update Departemen Sucsess... !');
			
			redirect('master/dept/');
		} else {
			$this->session->set_flashdata('message', 'Update Departemen gagal... !');
			redirect('master/dept/');
		}
		
	}
	function create_dept() {
		$this->data['h2_title'] = '';
		$this->data['title'] ='Create Department';
		$this->data['form_action'] = site_url('master/dept/create_dept');
		$xd = $this->Deptmodel->show_id_dept()->row();
		$this->data['default']['dept_id']= $xd->dept_id +1;
		
		$this->form_validation->set_rules('dept_id','Dept ID','required');
		$this->form_validation->set_rules('dept_name','Dept Name','required|callback_valid_deptname');
		if($this->form_validation->run() == TRUE) {
			$depts = array('dept_id'=>$this->input->post('dept_id'),
							'dept_name'=>strip_tags($this->input->post('dept_name')),
							
							'descr'=>strip_tags($this->input->post('descr')));
 			//insert departemen
			$this->Deptmodel->create_dept($depts);
			
			$this->session->set_flashdata('message', 'Create Departemen Sucsess... !');
			
			$jmlRow 	= $_POST['jmlrow'];
			for( $i=1; $i <= $jmlRow; $i++ ) {
					if( !empty( $jmlRow ) ) {					
						$qrun = mysql_query("INSERT INTO dept_detail VALUES('','".$this->input->post('dept_id')."', 
																	'".$_POST['prod_id'.$i]."')") or die(mysql_error());
					}
				}
			
			redirect('master/dept/');
		} else {
		}
		$this->data['link'] = array('link_back' =>anchor('master/dept','kembali',array('class' => 'back')));
		$this->data['prod'] = $this->Deptmodel->sProd();
		//$x = $this->Deptmodel->update_prod()->row();
		$this->data['prod_id'] = "";
		$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('dept/addDept');
			$this->load->view('layout/slice_bawah');	
		
		

	}
	function delete($id) {
		$this->Deptmodel->delete_dept($id); 
		$this->session->set_flashdata('message', 'Delete Departemen Sucsess... !');
		
		redirect('master/dept/');
			
	}
	function valid_deptname() {
		if(preg_match("/^[a-zA-z]+$/",$this->input->post('dept_name')))
		{
			return true;
			
			
		} else {
			$this->form_validation->set_message('valid_deptname', 'Dept Name hanya boleh huruf  ' );
			return false;
		}
		
	}
	function browsProd() {
		$this->load->vars($this->data);
			
			$this->load->view('dept/brws_prod');
		
	}
	function shdept($id) {
			$this->data['title'] ='Show Detail Departemen';
			$this->data['cek'] = $this->Deptmodel->showDept2($id);
			
			$_SESSION['id'] = $id;
			
			$dep =$this->Deptmodel->showDept($id)->result();
			$tmpl = array('table_open' =>'<table border="0" cellpadding="0" cellspacing="0"  id="table" class="sortable">', 'heading_row_start'   => '<thead><tr>',
                    'heading_row_end'     => '</tr></thead>',
                    'heading_cell_start'  => '<th>',
                    'heading_cell_end'    => '</th>','row_alt_start' =>'<tr class="tb-jd">','row_alt_end' =>'</tr>');
			$this->table->set_template($tmpl);
			
			//set heading untul tabel
			$this->table->set_empty("&nbsp;");
			$this->table->set_heading('No','<h3>Product</h3>','<h3>Actions</h3>');
			$i=0;
			foreach($dep as $dept) {
				
				$this->table->add_row(++$i,$dept->product,
				
				anchor('master/dept/deletedet/'.$dept->det_id,'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',array('class'=>'delete','onclick'=>"return confirm('Anda yakin akan menghapus data ini?')")));
			}
			
			$this->data['table'] = $this->table->generate();
			$this->data['link'] = array('link_back' =>anchor('master/dept','kembali',array('class' => 'back')));
			$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('dept/showDept');
			$this->load->view('layout/slice_bawah');	
	}
	function add_det() {
		if(isset($_POST['input'])) {
			$jmlRow 	= $_POST['jmlrow'];
				for( $i=1; $i <= $jmlRow; $i++ ) {
						if( !empty( $jmlRow ) ) {					
							$qrun = mysql_query("INSERT INTO dept_detail VALUES('','".$this->input->post('dept_id')."', 
																		'".$_POST['prod_id'.$i]."')") or die(mysql_error());
						}
					}
		}
		redirect('master/dept/shdept/'.$_SESSION['id']);
		
	}
	function deletedet($id) {
		$this->Deptmodel->delete_det($id); 
		$this->session->set_flashdata('message', 'Delete Departemen Sucsess... !');
		
		redirect('master/dept/shdept/'.$_SESSION['id']);
			
	}
}
?>